#include <QTimer>


#ifndef _ALARM_H_
#define _ALARM_H_

typedef struct _ThresholdAlarm {
    bool isAlarming = false;
    QDateTime alarmStartTime;
    bool thresholdExceeded = false;
    double currentValue = 0.0;
    double minThreshold = 0.0;
    double maxThreshold = 0.0;
    QString alarmName;

    bool checkAlarm(double newValue, const QDateTime& now) {
        bool wasExceeded = thresholdExceeded;
        currentValue = newValue;
        thresholdExceeded = (newValue < minThreshold || newValue > maxThreshold);

        // 关键修复：更新isAlarming状态
        if (thresholdExceeded && !wasExceeded) {
            isAlarming = true;
            alarmStartTime = now;
        } else if (!thresholdExceeded) {
            isAlarming = false;
            alarmStartTime = QDateTime();
        }

        return isAlarming;
    }
} ThresholdAlarm;


typedef struct _AlarmStatus {

        ThresholdAlarm ccd1;
        ThresholdAlarm ccd2;
        ThresholdAlarm ccd3;
        ThresholdAlarm ccd4;
        ThresholdAlarm base1;
        ThresholdAlarm base2;
        ThresholdAlarm absorbCold;
        ThresholdAlarm circuit;
        ThresholdAlarm dewar;
        ThresholdAlarm backup1;
        ThresholdAlarm backup2;
        ThresholdAlarm output;

        ThresholdAlarm vacuum;
        ThresholdAlarm current;


        QMap<QString, ThresholdAlarm*> allAlarms() {
            return {
                {"CCD1", &ccd1},
                {"CCD2", &ccd2},
                {"CCD3", &ccd3},
                {"CCD4", &ccd4},
                {"Base1", &base1},
                {"Base2", &base2},
                {"AbsorbCold", &absorbCold},
                {"Circuit", &circuit},
                {"Dewar", &dewar},
                {"Backup1", &backup1},
                {"Backup2", &backup2},
                {"Output", &output},
                {"Vacuum", &vacuum},
                {"Current", &current}
            };
        }
    } AlarmStatus;

#endif // _ALARM_H_